package it.cast.wechat.cotroller;

import it.cast.wechat.dto.ProductVo;
import it.cast.wechat.dto.Result;
import it.cast.wechat.pojo.Product;
import it.cast.wechat.service.ProductService;
import it.cast.wechat.utils.UploadUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Controller
public class ProductController {

    @Value("${fileRealPath}")
    private String fileRealPath;

    @Value("${fileSonPath}")
    private String fileSonPath;

    @Autowired
    private ProductService productService;

    @RequestMapping("product/saveProduct")
    public String addProduct(Product product, @RequestParam("upload") MultipartFile file){
        Optional<String> imageUrl = UploadUtils.uploadImage(file, fileRealPath, fileSonPath);
        imageUrl.ifPresent(product::setProductImage);
        productService.saveProduct(product);
        return "redirect:/product/queryAllProduct";
    }

    @RequestMapping("product/deleteProduct")
    public String deleteProduct(String productId){
        productService.deleteProduct(productId);
        return "redirect:/product/queryAllProduct";
    }

    @RequestMapping("product/queryProduct")
    public String queryProduct(String productId,Map<String,Object>map){
        Optional<Product> opt = productService.queryProduct(productId);
        map.put("product",opt.orElseGet(Product::new));
        return "admin/product/edit";
    }


    @RequestMapping("wx/product/queryProduct")
    @ResponseBody
    public Product wxQueryProduct(String productId){
        Optional<Product> opt = productService.queryProduct(productId);
        return opt.get();
    }

    @RequestMapping("product/queryAllProduct")
    public String queryAllProduct(Map<String,Object> map){
//        List<Product> productList = productService.queryAllProduct();
        List<ProductVo> productList = productService.queryAllProductWithCategory();
        map.put("productList",productList);
        return "admin/product/list";
    }

    @RequestMapping("product/queryProductByCategoryId")
    @ResponseBody
    public Result<List<Product>> queryProductByCategoryId(String categoryId){
        Result<List<Product>> result = new Result<>();
        List<Product> products = null;
        try {
            if(StringUtils.isNotEmpty(categoryId)){
                products = productService.queryProductByCategoryId(categoryId);
            }else {
                products = productService.queryAllProduct();
            }
            result.setData(products);
        }catch (Exception e){
            System.out.println(e.getMessage());
            result.setCode(100);
            result.setErrorMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("product/queryProductById")
    public Result<Product> queryProductById(String productId){
        Result<Product> result = new Result<>();
        try {
            Optional<Product> opt = productService.queryProduct(productId);
            opt.ifPresent(result::setData);
        }catch (Exception e){
            result.setCode(100);
            result.setErrorMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("product/getRecommend")
    @ResponseBody
    public Result<List<Product>> getRecommend(String userId){
        Result<List<Product>> result = new Result<>();
        List<Product> products = null;
        try {
            if(StringUtils.isNotEmpty(userId)){
                products = productService.getRecommend(userId);
            }else {
                products = productService.queryAllProduct();
            }
            result.setData(products);
        }catch (Exception e){
            System.out.println(e.getMessage());
            result.setCode(100);
            result.setErrorMsg(e.getMessage());
        }
        return result;

    }

}
